package com.ddyx.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.ddyx.entity.Goods;
import com.ddyx.entity.GoodsImage;
import com.ddyx.entity.GoodsStock;
import com.ddyx.entity.GoodsTagContact;
import com.ddyx.entity.Post;
import com.ddyx.entity.PostPic;
import com.ddyx.entity.user.UEmploye;
import com.ddyx.entity.user.UStore;
import com.ddyx.mapper.GoodsImageMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.GoodsStockMapper;
import com.ddyx.mapper.GoodsTagContactMapper;
import com.ddyx.mapper.PostMapper;
import com.ddyx.mapper.PostPicMapper;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.mapper.user.UEmployeMapper;
import com.ddyx.mapper.user.UStoreMapper;
import com.ddyx.service.IGoodsService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.EventTag;
import com.ddyx.util.MQUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.GoodsVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class GoodsServiceImpl implements IGoodsService {

	@Resource
	GoodsMapper goodsMapper;

	@Resource
	StoreMapper storeMapper;
	@Resource
	UStoreMapper usMapper;

	@Resource
	GoodsImageMapper goodsImageMapper;

	@Resource
	PostMapper postMapper;

	@Resource
	PostPicMapper postPicMapper;
	
	@Resource
	GoodsTagContactMapper gtcMapper;

	@Resource
	UEmployeMapper uEmployeMapper;
	
	@Resource
	GoodsStockMapper goodsStockMapper;
	
	@Autowired
	JmsTemplate jmsTemplate;
	
	@Autowired
	Destination topicDestination;
	
	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm addGoodsBysSalesclerk(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				Goods bean = new Goods();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();

				String s = paramMap.get("goodsPrice") + "";
				
				String tagId = (String)paramMap.get("tagId");
				
				if (paramMap.containsKey("goodsPrice") && StringUtils.hasText(s) && !"null".equals(s)) {
					BigDecimal bd = new BigDecimal(s);
					// bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP);
					paramMap.put("goodsPrice", bd);
				} else {
					paramMap.put("goodsPrice", 0);
				}

				String discountPrice = paramMap.get("discountPrice") + "";
				if (paramMap.containsKey("discountPrice") && StringUtils.hasText(discountPrice)
						&& !"null".equals(discountPrice)) {
					BigDecimal bdq = new BigDecimal(discountPrice);
					// bdq=bdq.setScale(2, BigDecimal.ROUND_HALF_UP);
					paramMap.put("discountPrice", bdq);
					paramMap.put("discountStatus", 1);
				} else {
					paramMap.put("discountPrice", 0);
					paramMap.put("discountStatus", 0);
				}

				MyBeanUtils.populate(bean, paramMap);
				/*
				 * GoodsVo gvo = new GoodsVo(); gvo.setWxUid((String) paramMap.get("wxUid"));
				 * gvo = goodsMapper.selectEmployeByWXUid(gvo); if(null==gvo) {
				 * result.setStatus(false); result.setMessage("没有权限"); return result; }
				 */
				/*Store store = new Store();
				store.setStoreNo(bean.getStoreNo());
				Store st = storeMapper.selectOne(store);
				if (null == st || null == st.getStoreId()) {
					result.setStatus(false);
					result.setMessage("storeNo对应的门店不存在");
					return result;
				}*/
				UStore store = new UStore();
				store.setStoreNo(bean.getStoreNo());
				store = usMapper.selectOne(store);
				if (null == store || null == store.getStoreId()) {
					result.setStatus(false);
					result.setMessage("storeNo对应的门店不存在");
					return result;
				}
				bean.setStoreId(store.getStoreId());
				bean.setSoldonTime(new Date());
				if (null == bean.getCommendStatus()) {
					bean.setCommendStatus(0);
				}
				/*
				 * if(null!=bean.getDiscountPrice() && new
				 * BigDecimal("0")!=bean.getDiscountPrice()) { bean.setDiscountStatus(1); }else
				 * { bean.setDiscountStatus(0); }
				 */
				bean.setSoldout(0);

				bean.setFindStatus((Integer) paramMap.get("findStatus"));

				String imgUrls = bean.getGoodsUrl();
				if(!StringUtils.hasText(imgUrls)) {
					result.setStatus(false);
					result.setMessage("商品图片不能为空!");
					return result;
				}
				String[] imgUrlses = imgUrls.split(",");
				bean.setGoodsPic(imgUrlses[0]);
				bean.setGoodsUrl("");
				goodsMapper.insertSelective(bean);
				int in = bean.getGoodsId();
				// System.out.println(in);
				List<GoodsImage> list = new ArrayList<GoodsImage>();
				for (int i = 0; i < imgUrlses.length; i++) {
					GoodsImage gi = new GoodsImage();
					String imgUrl = imgUrlses[i];
					gi.setGoodsId(in);
					gi.setImageUrl(imgUrl);
					list.add(gi);
				}
				int ii = goodsImageMapper.addGoodsImageBysSalesclerk(list);
				System.out.println(ii);

				if (0 == (bean.getFindStatus())) {
					synchroPost(bean, imgUrlses);
				}
				
				 
				//标签商品关系同步
				if(!"".equals(tagId)) {
					GoodsTagContact gtc = new GoodsTagContact();
//					gtc.setTagId((Integer)paramMap.get("tagId"));
					gtc.setTagId(Integer.valueOf(tagId));
					gtc.setGoodsId(bean.getGoodsId());
					gtcMapper.insertSelective(gtc);
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	/**
	 * @DESC 新增商品如果开启发现则同步到post帖子事件中
	 * @param paramMap
	 */
	@Transactional
	private void synchroPost(Goods goods, String[] imgUrlses) {
		try {
			if (goods != null && imgUrlses.length > 0) {
				Post post = new Post();
				post.setCreateTime(new Date());
				// post.setHotTopicId();
				// post.setEmployNo(paramMap.get("employeNo"));
				post.setPostContent(goods.getGoodsName());
				post.setStoreNo(goods.getStoreNo());
				postMapper.insertSelective(post);
				if (post.getPostId() != null) {
					PostPic postPic = new PostPic();
					postPic.setGoodsId(goods.getGoodsId());
					postPic.setPostId(post.getPostId());
					for (String string : imgUrlses) {
						postPic.setPostPic(string);
						postPicMapper.insertSelective(postPic);
						postPic.setPostPicId(null);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("操作有误");
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectGoodsListBysSalesclerk(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				GoodsVo bean = new GoodsVo();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				MyBeanUtils.populate(bean, paramMap);
				if(paramMap.get("soldout")==null||paramMap.get("soldout")=="") {
					bean.setSoldout(null);
				}
				if (!StringUtils.hasText(bean.getStoreNo())) {
					result.setStatus(false);
					result.setMessage("storeno参数没有值");
					return result;
				}
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<GoodsVo> list = goodsMapper.selectGoodsListBysSalesclerk(bean);
				if (null != list && list.size() > 0) {
					for (int i = 0; i < list.size(); i++) {
						GoodsVo gg = list.get(i);
						String tim = gg.getSoldonTime();
						/*
						 * String goodsPrice=gg.getGoodsPrice(); if(StringUtils.hasText(goodsPrice)) {
						 * goodsPrice = goodsPrice.substring(0,goodsPrice.length() - 3);
						 * gg.setGoodsPrice(goodsPrice); }
						 */
						if (StringUtils.hasText(tim)) {
							tim = tim.substring(0, tim.length() - 11);
							gg.setSoldonTime(tim);
						}
					}
				}
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	/*
	 * 获取多门店首页顶部信息
	 */
	@Override
	public ResponseForm getIndexTop(RequestForm param) {
		ResponseForm result = new ResponseForm();
		// Map<String, Object> parmMap = (Map<String, Object>) param.getData();
		try {
			// Example example = new Example(Goods.class);
			// example.selectProperties("goodsId").selectProperties("goodsPic").selectProperties("goodsName")
			// .selectProperties("goodsPrice");
			// List<Goods> list = goodsMapper.selectByExampleAndRowBounds(example,
			// rowBounds);
			RowBounds rowBounds = new RowBounds(1, 3);
			List<Goods> list = goodsMapper.selectByRowBounds(new Goods(), rowBounds);
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm deleteGoodsBysSalesclerk(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				Goods bean = new Goods();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				MyBeanUtils.populate(bean, paramMap);
				if (null != bean && null != bean.getGoodsId()) {
					int in = goodsMapper.delete(bean);
					GoodsImage goodsImage = new GoodsImage();
					goodsImage.setGoodsId(bean.getGoodsId());
					int ii = goodsImageMapper.delete(goodsImage);
					if (0 < in && 0 < ii) {
						return result;
					}
				}
				result.setStatus(false);
				result.setMessage("操作有误");
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm updateGoodsBysSalesclerk(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				Goods bean = new Goods();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				String s = paramMap.get("goodsPrice") + "";
				
				String tagId=String.valueOf(paramMap.get("tagId"));
				
				
				if (paramMap.containsKey("goodsPrice") && StringUtils.hasText(s) && !"null".equals(s)) {
					BigDecimal bd = new BigDecimal(s);
					// bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP);
					paramMap.put("goodsPrice", bd);
				} else {
					paramMap.put("goodsPrice", 0);
				}

				String discountPrice = paramMap.get("discountPrice") + "";
				System.out.println(discountPrice);
				if (paramMap.containsKey("discountPrice") && StringUtils.hasText(discountPrice)
						&& !"null".equals(discountPrice)) {
					BigDecimal bdq = new BigDecimal(discountPrice);
					// bdq=bdq.setScale(2, BigDecimal.ROUND_HALF_UP);
					paramMap.put("discountPrice", bdq);
					paramMap.put("discountStatus", 1);
				} else {
					paramMap.put("discountPrice", 0);
					paramMap.put("discountStatus", 0);
				}

				MyBeanUtils.populate(bean, paramMap);

				if (null == bean.getCommendStatus()) {
					bean.setCommendStatus(0);
				}

				// 发现字段
				if (null == bean.getFindStatus()) {
					bean.setFindStatus(0);
				}

				/*
				 * if(null!=bean.getDiscountPrice() && new
				 * BigDecimal("0")!=bean.getDiscountPrice() && new
				 * BigDecimal("0.0")!=bean.getDiscountPrice() && new
				 * BigDecimal("0.00")!=bean.getDiscountPrice() ) { bean.setDiscountStatus(1);
				 * }else { bean.setDiscountStatus(0); }
				 */
				String imgUrls = bean.getGoodsUrl();
				if(!StringUtils.hasText(imgUrls)) {
					result.setStatus(false);
					result.setMessage("商品图片不能为空!");
					return result;
				}
				String[] imgUrlses = imgUrls.split(",");
				bean.setGoodsPic(imgUrlses[0]);
				bean.setGoodsUrl("");
				goodsMapper.updateByPrimaryKeySelective(bean);

				GoodsImage goodsImage = new GoodsImage();
				goodsImage.setGoodsId(bean.getGoodsId());
				int ii = goodsImageMapper.delete(goodsImage);

				if (0 < ii) {
					List<GoodsImage> list = new ArrayList<GoodsImage>();
					for (int i = 0; i < imgUrlses.length; i++) {
						GoodsImage gi = new GoodsImage();
						String imgUrl = imgUrlses[i];
						gi.setGoodsId(bean.getGoodsId());
						gi.setImageUrl(imgUrl);
						list.add(gi);
					}
					int iq = goodsImageMapper.addGoodsImageBysSalesclerk(list);
					System.out.println(iq);
				}
				
				
				//商品标签关系同步
				if(!"".equals(tagId)) {
					GoodsTagContact gtc = new GoodsTagContact();
					gtc.setGoodsId((Integer) paramMap.get("goodsId"));
					gtcMapper.delete(gtc);
					gtc.setTagId(Integer.valueOf(tagId));
					gtcMapper.insertSelective(gtc);
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getHotGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getHotGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getNewGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getNewGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getCommendGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getCommendGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getDiscountGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getDiscountGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		Boolean bo = map.containsKey("kwe");
		System.out.println(bo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getFindGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getFindGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getActivityGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.getActivityGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm searchGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Map<String, Object>> list = goodsMapper.searchGoods(paramMap);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getGoodsListByIds(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();

				Example example = new Example(Goods.class);

				Criteria c = example.createCriteria();
				Iterable<Long> it = (Iterable<Long>) paramMap.get("ids");
				c.andEqualTo("storeNo", paramMap.get("storeNo"));
				c.andIn("goodsId", it);

				List<Goods> list = goodsMapper.selectByExample(example);

				// List<Map<String, Object>> list = goodsMapper.searchGoods(paramMap);
				result.setData(list);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm updateGoodsStatus(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData()!=null) {
			try {
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				String idsStr = paramMap.get("ids")+"";
				String[] ids = idsStr.split(",");
				Goods good = new Goods();
				GoodsTagContact gtc = new GoodsTagContact();
				//批量下架状态更新
				if (EventTag.SOLD_OUT.equals(paramMap.get("actionType"))) {
					for (String idStr : ids) {
						int id = Integer.parseInt(idStr);
						good.setGoodsId(id);
						good.setSoldout(1);
						//更新数据库
						goodsMapper.updateByPrimaryKeySelective(good);
						//更新商品标签关系
						gtc.setGoodsId(id);
						gtcMapper.delete(gtc);
					}
				}
				//批量上架状态更新
				if (EventTag.SOLD_ON.equals(paramMap.get("actionType"))) {
					for (String idStr : ids) {
						int id = Integer.parseInt(idStr);
						good.setGoodsId(id);
						good.setSoldout(0);
						//更新数据库
						goodsMapper.updateByPrimaryKeySelective(good);
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getGoodInfo(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map map = (Map) param.getData();
			Integer goodsId = (Integer) map.get("goodsId");
			Goods good = new Goods();
			good.setGoodsId(goodsId);
			GoodsImage goodsImage = new GoodsImage();
			goodsImage.setGoodsId(goodsId);
			try {
				good = goodsMapper.selectOne(good);
				List<GoodsImage> goodsImages = goodsImageMapper.select(goodsImage);
				good.setGoodsImages(goodsImages);
				//标签
				GoodsTagContact gtc = new GoodsTagContact();
				
					gtc.setGoodsId(goodsId);
					gtc = gtcMapper.selectOne(gtc);
				if(gtc!=null) {
					good.setTagId(gtc.getTagId());
				}
				result.setData(good);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getGoodsListByTag(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> page = PageHelper.startPage((Integer)paramMap.get("page"), (Integer)paramMap.get("size"));
				List<Map> goodsList = goodsMapper.getGoodsListByTag(paramMap);
				result.setData(goodsList);
				result.setTotal((int) page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}
	
	
	/*
	 * 获取多门店首页 多门店商品展示信息
	 */
	@Override
	public ResponseForm getGoodsShow(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String[] storeNos = paramMap.get("storeNos").toString().split(",");
			Goods goods = new Goods();
			List<LinkedHashMap> storeList = new ArrayList<>();
			LinkedHashMap storeInfo = new LinkedHashMap<>();
			for (int i = 0; i < storeNos.length; i++) {
				storeInfo = usMapper.selectAllBySelf((String) storeNos[i]);
				if (storeInfo!=null) {
					storeList.add(storeInfo);storeInfo = null;
				}
			}
//			storeList = usMapper.selectAllBySelf();
			for (LinkedHashMap store : storeList) {
				goods.setStoreNo((String) store.get("storeNo"));
				// RowBounds rowBounds = new RowBounds(1, 4);
				List<Goods> goodsList = goodsMapper.selectByRowBounds(goods, new RowBounds(1, 4));
				store.put("goodsList", goodsList);
				goods.setStoreNo(null);// 置空
			}
			result.setData(storeList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getGoodDetail(RequestForm param) {

		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			String employeNo =(String) paramMap.get("employeNo");
			String goodsId =(String) paramMap.get("goodsId");
			String wxUid =(String) paramMap.get("wxUid");
			if(!StringUtils.hasText(goodsId)||!StringUtils.hasText(storeNo)){
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
				return result;
			}
			
			if(!StringUtils.hasText(employeNo)){
				UEmploye employe = new UEmploye();
				employe.setStoreNo(storeNo);
				employe.setRoleId(1);
				List<UEmploye> eList=uEmployeMapper.select(employe);
				if(eList!=null && eList.size()>0){
					employe = eList.get(0);
					employeNo =employe.getEmployeNo();
				}else{
					employeNo ="0";
					result.setStatus(false);
					result.setMessage("导购信息有误");
					log.error("导购信息有误");
					return result;
				}
			}
			
			Goods goods = goodsMapper.selectByPrimaryKey(Integer.valueOf(goodsId));
			
			//处理商品图片
			GoodsImage goodsImage =new GoodsImage();
			goodsImage.setGoodsId(Integer.valueOf(goodsId));
			List<GoodsImage> goodsImageList=goodsImageMapper.select(goodsImage);
			goods.setGoodsImages(goodsImageList);
			//商品颜色
			List<Map<String, Object>> goodsColour =null ;
			String relation = goods.getRelation();
			if(relation!=null &&!"".equals(relation)){
				Map<String,Object> relationMap = new HashMap<String, Object>();
				relationMap.put("storeNo", storeNo);
				relationMap.put("relation", relation);
				goodsColour =goodsMapper.getGoodsColourList(relationMap);
			}else{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("goods_name", goods.getGoodsName());
				map.put("goods_code", goods.getBarcode());
				map.put("goods_color", goods.getGoodsColor());
				map.put("goods_id", goods.getGoodsId());
				map.put("soldout", goods.getSoldout());
				goodsColour = new ArrayList<Map<String, Object>>();
				goodsColour.add(map);
			}
			goods.setGoodsColour(goodsColour);
			// 商品尺寸
			GoodsStock goodsStock =new GoodsStock();
			goodsStock.setGoodsId(Integer.valueOf(goodsId));
			List<GoodsStock> goodsStockList=goodsStockMapper.select(goodsStock);
			goods.setGoodsStock(goodsStockList);
			result.setData(goods);
			//记录商品浏览记录
			//新增门店客户
			//打点数据
			if(StringUtils.hasText(wxUid)){
				//新增门店客户
				Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.NEWCUSTOMER);
				cMap.put("createTime", new Date());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
				//浏览首页
				//Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.BROWSE);
				cMap.put("eventTime", new Date());
				cMap.put("goodsPic", goods.getGoodsPic());
				cMap.put("goodsName", goods.getGoodsName());
				cMap.put("goodsId", goods.getGoodsId());
				cMap.put("goodsPrice", goods.getGoodsPrice().toString());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public ResponseForm addGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			List paramList = (List) paramMap.get("paramList");
			if(!StringUtils.hasText(storeNo)||paramList==null){
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
				return result;
			}
			//原价
			String goodsPrice = (String) paramMap.get("goodsPrice");
			BigDecimal bd = new BigDecimal(goodsPrice);
			paramMap.put("goodsPrice", bd);
			//折扣价
			String discountPrice = (String) paramMap.get("discountPrice");
			if(StringUtils.hasText(discountPrice)){
				BigDecimal discountbd = new BigDecimal(discountPrice);
				paramMap.put("discountPrice", discountbd);
			}else{
				paramMap.put("discountPrice", 0);
			}
			
			String relation="";
			//同款不同色商品关系字段
			if(paramList.size()>1){
				relation=String.valueOf(System.currentTimeMillis());
			}
			
			for (int i = 0; i < paramList.size(); i++) {
				Goods goods = new Goods();
				MyBeanUtils.populate(goods, paramMap);
				goods.setRelation(relation);
				Map<String, Object> map =(Map<String, Object>) paramList.get(i);
				goods.setGoodsColor((String)map.get("goodsColour"));
				goods.setBarcode((String)map.get("goodsCode"));
				goods.setSoldonTime(new Date());
				goods.setCommendStatus(0);
				goods.setSoldoutTime(DateUtil.toDate( map.get("soldoutTime")));
				goods.setGoodsDesc((String)map.get("goodsDesc"));
				goods.setFindStatus(0);
				List imgList =(List) map.get("goodsImg");
//				if(imgList!=null && imgList.size()>0)
//				goods.setGoodsPic((String)imgList.get(0));
				goodsMapper.insert(goods);
				if(imgList.size()>0){
					GoodsImage gImg;
					for (int j = 0; j < imgList.size(); j++) {
						gImg= new GoodsImage();
						gImg.setGoodsId(goods.getGoodsId());
						gImg.setImageUrl((String) imgList.get(j));
						goodsImageMapper.insert(gImg);
					}
				}
				List sizeList =(List) map.get("goodsSize");
				if(sizeList.size()>0){
					GoodsStock gStock;
					for (int k = 0; k < sizeList.size(); k++) {
						Map<String, Object> stockMap =(Map<String, Object>) sizeList.get(k);
						gStock= new GoodsStock();
						gStock.setGoodsId(goods.getGoodsId());
						gStock.setSpecsName((String) stockMap.get("size"));
						String num =(String) stockMap.get("num");
						gStock.setStockNum(Integer.valueOf(num));
						goodsStockMapper.insert(gStock);
					}
				}
			}
			
			
		} catch (Exception e) {
			result.setStatus(false);
			e.printStackTrace();
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public ResponseForm updateGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			String goodsId = (String) paramMap.get("goodsId");
			if(!StringUtils.hasText(storeNo)||!StringUtils.hasText(goodsId)){
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
				return result;
			}
			//原价
			String goodsPrice = (String) paramMap.get("goodsPrice");
			BigDecimal bd = new BigDecimal(goodsPrice);
			paramMap.put("goodsPrice", bd);
			//折扣价
			String discountPrice = (String) paramMap.get("discountPrice");
			if(StringUtils.hasText(discountPrice)){
				BigDecimal discountbd = new BigDecimal(discountPrice);
				paramMap.put("discountPrice", discountbd);
			}else{
				paramMap.put("discountPrice", 0);
			}
			Goods goods = new Goods();
			MyBeanUtils.populate(goods, paramMap);
			//修改图片信息
			List imgList =(List) paramMap.get("goodsImg");
			if(imgList!=null && imgList.size()>0){
				GoodsImage gImg= new GoodsImage();
				gImg.setGoodsId(goods.getGoodsId());
				goodsImageMapper.delete(gImg);
				for (int j = 0; j < imgList.size(); j++) {
					gImg= new GoodsImage();
					gImg.setGoodsId(goods.getGoodsId());
					gImg.setImageUrl((String) imgList.get(j));
					goodsImageMapper.insert(gImg);
				}
			}
			//修改尺码信息
			List sizeList =(List) paramMap.get("goodsSize");
			if(sizeList!=null && sizeList.size()>0){
				GoodsStock gStock = new GoodsStock();
				gStock.setGoodsId(goods.getGoodsId());
				goodsStockMapper.delete(gStock);
				for (int k = 0; k < sizeList.size(); k++) {
					Map<String, Object> stockMap =(Map<String, Object>) sizeList.get(k);
					gStock= new GoodsStock();
					gStock.setGoodsId(goods.getGoodsId());
					gStock.setSpecsName((String) stockMap.get("size"));
					String num =(String) stockMap.get("num");
					gStock.setStockNum(Integer.valueOf(num));
					goodsStockMapper.insert(gStock);
				}
			}
			//下架时间
			if(goods.getSoldout()!=null && goods.getSoldout()==1){
				goods.setSoldoutTime(new Date());
			}
			
			goodsMapper.updateByPrimaryKeySelective(goods);
			
			
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("操作错误");
			e.printStackTrace();
			log.error("操作错误");
		}
		return result;
	}
}
